home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / var / lib / python-support / python2.6 / orca / input_event.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  7.6 KB  |  176 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. '''Provides support for handling input events.  This provides several classes
  5. to define input events (InputEvent, KeyboardEvent, BrailleEvent,
  6. MouseButtonEvent, MouseMotionEvent, and SpeechEvent), and also provides a
  7. InputEventHandler class.  It is intended that instances of InputEventHandler
  8. will be used which should be used to handle all input events.'''
  9. __id__ = '$Id: input_event.py 4670 2009-04-10 13:10:23Z wwalker $'
  10. __version__ = '$Revision: 4670 $'
  11. __date__ = '$Date: 2009-04-10 09:10:23 -0400 (Fri, 10 Apr 2009) $'
  12. __copyright__ = 'Copyright (c) 2005-2008 Sun Microsystems Inc.'
  13. __license__ = 'LGPL'
  14. import debug
  15. import settings
  16. import time
  17. KEYBOARD_EVENT = 'keyboard'
  18. BRAILLE_EVENT = 'braille'
  19. MOUSE_BUTTON_EVENT = 'mouse:button'
  20. MOUSE_MOTION_EVENT = 'mouse:motion'
  21. SPEECH_EVENT = 'speech'
  22.  
  23. class InputEvent:
  24.     
  25.     def __init__(self, eventType):
  26.         '''Creates a new input event of the given type.
  27.  
  28.         Arguments:
  29.         - eventType: the input event type (one of KEYBOARD_EVENT, BRAILLE_EVENT,
  30.                 MOUSE_BUTTON_EVENT, MOUSE_MOTION_EVENT, SPEECH_EVENT).
  31.         '''
  32.         self.type = eventType
  33.  
  34.  
  35.  
  36. class KeyboardEvent(InputEvent):
  37.     
  38.     def __init__(self, event):
  39.         '''Creates a new InputEvent of type KEYBOARD_EVENT.
  40.  
  41.         Arguments:
  42.         - event: the AT-SPI keyboard event
  43.         '''
  44.         event_string = event.event_string
  45.         if event.modifiers & settings.CTRL_MODIFIER_MASK and not (event.is_text) and len(event_string) == 1:
  46.             value = ord(event.event_string[0])
  47.             if value < 32:
  48.                 event_string = chr(value + 64)
  49.             
  50.         
  51.         event.modifiers = event.modifiers & settings.ALL_BUT_NUMLOCK_MODIFIER_MASK
  52.         InputEvent.__init__(self, KEYBOARD_EVENT)
  53.         self.type = event.type
  54.         self.hw_code = event.hw_code
  55.         self.modifiers = event.modifiers
  56.         self.event_string = event_string
  57.         self.is_text = event.is_text
  58.         self.time = time.time()
  59.         self.timestamp = event.timestamp
  60.  
  61.  
  62.  
  63. class BrailleEvent(InputEvent):
  64.     
  65.     def __init__(self, event):
  66.         '''Creates a new InputEvent of type BRAILLE_EVENT.
  67.  
  68.         Arguments:
  69.         - event: the integer BrlTTY command for this event.
  70.         '''
  71.         InputEvent.__init__(self, BRAILLE_EVENT)
  72.         self.event = event
  73.  
  74.  
  75.  
  76. class MouseButtonEvent(InputEvent):
  77.     
  78.     def __init__(self, event):
  79.         '''Creates a new InputEvent of type MOUSE_BUTTON_EVENT.
  80.         '''
  81.         InputEvent.__init__(self, MOUSE_BUTTON_EVENT)
  82.         self.x = event.detail1
  83.         self.y = event.detail2
  84.         self.pressed = event.type.endswith('p')
  85.         self.button = event.type[len('mouse:button:'):-1]
  86.         self.time = time.time()
  87.  
  88.  
  89.  
  90. class MouseMotionEvent(InputEvent):
  91.     
  92.     def __init__(self, event):
  93.         '''[[[TODO: WDW - undefined at the moment.]]]
  94.         '''
  95.         InputEvent.__init__(self, MOUSE_MOTION_EVENT)
  96.         self.event = event
  97.  
  98.  
  99.  
  100. class SpeechEvent(InputEvent):
  101.     
  102.     def __init__(self, event):
  103.         '''[[[TODO: WDW - undefined at the moment.]]]
  104.         '''
  105.         InputEvent.__init__(self, SPEECH_EVENT)
  106.         self.event = event
  107.  
  108.  
  109.  
  110. class InputEventHandler:
  111.     
  112.     def __init__(self, function, description, learnModeEnabled = True):
  113.         '''Creates a new InputEventHandler instance.  All bindings
  114.         (e.g., key bindings and braille bindings) will be handled
  115.         by an instance of an InputEventHandler.
  116.  
  117.         Arguments:
  118.         - function: the function to call with an InputEvent instance as its
  119.                     sole argument.  The function is expected to return True
  120.                     if it consumes the event; otherwise it should return
  121.                     False
  122.         - description: a localized string describing what this InputEvent
  123.                        does
  124.         - learnModeEnabled: if True, the description will be spoken and
  125.                             brailled if learn mode is enabled.  If False,
  126.                             the function will be called no matter what.
  127.         '''
  128.         self.function = function
  129.         self.description = description
  130.         self._learnModeEnabled = learnModeEnabled
  131.  
  132.     
  133.     def __eq__(self, other):
  134.         '''Compares one input handler to another.'''
  135.         return self.function == other.function
  136.  
  137.     
  138.     def processInputEvent(self, script, inputEvent):
  139.         '''Processes an input event.  If settings.learnModeEnabled is True,
  140.         this will merely report the description of the input event to braille
  141.         and speech.  If settings.learnModeEnabled is False, this will call the
  142.         function bound to this InputEventHandler instance, passing the
  143.         inputEvent as the sole argument to the function.
  144.  
  145.         This function is expected to return True if it consumes the
  146.         event; otherwise it is expected to return False.
  147.  
  148.         Arguments:
  149.         - script:     the script (if any) associated with this event
  150.         - inputEvent: the input event to pass to the function bound
  151.                       to this InputEventHandler instance.
  152.         '''
  153.         consumed = False
  154.         if settings.learnModeEnabled and self._learnModeEnabled:
  155.             if self.description:
  156.                 import braille
  157.                 import speech
  158.                 braille.displayMessage(self.description)
  159.                 speech.speak(self.description)
  160.                 consumed = True
  161.             
  162.         else:
  163.             
  164.             try:
  165.                 consumed = self.function(script, inputEvent)
  166.             except:
  167.                 debug.printException(debug.LEVEL_SEVERE)
  168.  
  169.         return consumed
  170.  
  171.  
  172.  
  173. def keyEventToString(event):
  174.     return 'KEYEVENT: type=%d\n' % event.type + '          hw_code=%d\n' % event.hw_code + '          modifiers=%d\n' % event.modifiers + '          event_string=(%s)\n' % event.event_string + '          is_text=%s\n' % event.is_text + '          time=%f' % time.time()
  175.  
  176.